//
//  DetailViewController.m
//  quanminzhekou
//
//  Created by zxl on 15/1/8.
//  Copyright (c) 2015年 zhongqinglongtu. All rights reserved.
//

#import "DetailViewController.h"
#import "CollectManager.h"

@interface DetailViewController () 
@property (nonatomic) UIControl* markCtrl;
@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setSubViewHander];
    
    NSURLRequest* request = [NSURLRequest requestWithURL:self.currentGoodsViewModel.url];
    [self.webView loadRequest:request];
    
    self.toolBar.collectBtn.selected = [[CollectManager shareManager] isCollectedGoods:self.currentGoodsViewModel];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    if (nil == _markCtrl) {
        _markCtrl = [[UIControl alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _markCtrl.backgroundColor = [UIColor blackColor];
        _markCtrl.alpha = .0;
        _markCtrl.hidden = YES;
        [_markCtrl addTarget:self action:@selector(closeShareView) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_markCtrl];
        [self.view bringSubviewToFront:_markCtrl];
        
        [self.view bringSubviewToFront:self.shareView];
    }
    
    [self.activityView startAnimating];
    
    if (!UIEdgeInsetsEqualToEdgeInsets(self.webView.scrollView.contentInset, UIEdgeInsetsZero)) {
        self.webView.scrollView.contentInset = UIEdgeInsetsZero;//短信等分享后，webview会下移
    }
    
    [self.shareView setupShareModelsArr];
}

- (ShareView*)shareView {
    if (nil == _shareView) {
        _shareView = [[ShareView alloc] init];
        _shareView.currentGoodsViewModel = self.currentGoodsViewModel;
        [self.view addSubview:_shareView];
        
        __weak typeof(self) weakSelf = self;
        _shareView.tapCancelHandler = ^() {
            [weakSelf closeShareView];
        };
    }
    
    return _shareView;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
}

#pragma mark -
- (void)setSubViewHander {
    __weak typeof(self) weakSelf = self;
    self.toolBar.backBtnHandler = ^(UIButton* btn) {
        [weakSelf.navigationController popViewControllerAnimated:YES];
    };
    
    self.toolBar.leftBtnHandler = ^(UIButton* btn) {
        [weakSelf.webView goBack];
    };
    
    self.toolBar.rightBtnHandler = ^(UIButton* btn) {
        [weakSelf.webView goForward];
    };
    
    self.toolBar.collectBtnHandler = ^(UIButton* btn) {
        if (btn.selected) {
            [[CollectManager shareManager] addCollectedGoods:weakSelf.currentGoodsViewModel];
        } else {
            [[CollectManager shareManager] removeCollectGoods:weakSelf.currentGoodsViewModel];
        }
    };
    
    self.toolBar.shareBtnHandler = ^(UIButton* btn) {
        weakSelf.shareView.hidden = NO;
        weakSelf.markCtrl.hidden = NO;
        [weakSelf showShareView];
    };
    
    self.shareView.tapCancelHandler = ^() {
        [weakSelf closeShareView];
    };
}

- (void)showShareView {
    if (nil == self.shareView.ownedController) {
        self.shareView.ownedController = self;
    }
    
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:.5 animations:^{
        weakSelf.shareView.transform = CGAffineTransformMakeTranslation(0, -CGRectGetHeight(weakSelf.shareView.bounds));
        weakSelf.markCtrl.alpha = .3;
    }];
}

- (void)closeShareView {
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:.5 animations:^{
        weakSelf.shareView.transform = CGAffineTransformIdentity;
        weakSelf.markCtrl.alpha = 0;
    } completion:^(BOOL finished) {
        weakSelf.shareView.hidden = YES;
        weakSelf.markCtrl.hidden = YES;
        weakSelf.shareView.ownedController = nil;
    }];
}

#pragma mark - NSNotificationCenter
- (void)notificationWillEnterForeground:(id)sender {
    [self.shareView setupShareModelsArr];
}
- (void)notificationDidEnterBackground:(id)sender {
    [self closeShareView];
}

#pragma mark - UIWebViewDelegate
- (void)webViewDidStartLoad:(UIWebView *)webView {
    if (self.activityView.hidden) {
        self.activityView.hidden = NO;
        [self.activityView startAnimating];
    }
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    self.toolBar.leftBtn.enabled = webView.canGoBack;
    self.toolBar.rightBtn.enabled = webView.canGoForward;
    self.activityView.hidden = YES;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    self.activityView.hidden = YES;
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    webView.scrollView.contentInset = UIEdgeInsetsZero;//调整webview内容展示的偏移。ios7默认是{20, 0, 0, 0}
    return YES;
}

@end
